package com.hypo.interception;

import com.hypo.until.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
//@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //4.获取请求头令牌
        String token = request.getHeader("token");
        //5.令牌校验（令牌为空，或者令牌不通过，都要拦截）
        if (ObjectUtils.isEmpty(token) || JwtUtils.parseJWT(token) == null) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return false;
        }
        //6.校验通过，放行请求
        return true;
    }
}
